package com.jnu.myfirstapplication.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.SurfaceControlViewHost;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import androidx.annotation.NonNull;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class GameView extends SurfaceView implements SurfaceHolder.Callback{
    private DrawThread drawThread=null;
    private SurfaceHolder surfaceHolder;

    private ArrayList<Spriter> spriterArrayList=new ArrayList<>();
    public GameView(Context context) {
        super(context);
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView(){
        surfaceHolder=this.getHolder();
        surfaceHolder.addCallback(this);
    }
    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
        for(int i=0;i<5;i++){
            Spriter spriter=new Spriter(this.getContext());
            spriter.setX(i*50);
            spriter.setY(i*50);
            spriter.setDirection((float) (Math.random()*2*Math.PI));
            spriterArrayList.add(spriter);
        }
        drawThread=new DrawThread();
        drawThread.start();
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

    }


    class DrawThread extends Thread {
        private boolean isDrawing=true;
        @Override
        public void run() {
            while(isDrawing){
                Canvas canvas=null;
                try{
                    canvas=surfaceHolder.lockCanvas();
                    canvas.drawColor(Color.GRAY);
                    for(Spriter spriter:spriterArrayList){
                        spriter.move(canvas.getHeight(),canvas.getWidth());
                    }
                    for(Spriter spriter:spriterArrayList){
                        spriter.draw(canvas);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    if(null!=canvas){
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
                //drawing
                try{
                    sleep(20);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

        public void stopThread(){
            isDrawing=false;
            try{
                join();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
